<script setup>
import {ref} from "vue";
import api from "@/api/index.js";
import {reqLogin} from "@/api/modules/login.js";
import router from "@/routers/index.js";

const checkCodeURL = ref(api.generateCaptchaURL)
const changeCheckCode = () => {
  checkCodeURL.value = api.generateCaptchaURL + "?time=" + new Date().getTime()
}
const formRef = ref()
const loginModel = ref({
  username: '',
  password: '',
  checkCode: ''
})
const login = async () => {
  const success = await formRef.value.validate()
  if (success) {
    await reqLogin(loginModel.value)
    await router.push("/")
  }
}
</script>

<template>
  <div class="bg">
    <el-form class="login-form" ref="formRef" :model="loginModel">
      <div class="login-title">登录页面</div>
      <el-form-item>
        <el-input placeholder="用户名" v-model="loginModel.username">
          <template #prefix>
            <span class="iconfont icon-account"></span>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-input placeholder="密码" v-model="loginModel.password">
          <template #prefix>
            <span class="iconfont icon-password"></span>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <div class="check-code">
          <el-input class="input" placeholder="验证码" v-model="loginModel.checkCode"></el-input>
          <el-image class="pic" :src="checkCodeURL" @click="changeCheckCode"></el-image>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button class="login-btn" type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<style lang="scss" scoped>
.bg {
  height: 100vh;
  width: 100vw;
  background-image: url("../../assets/pic/login-bg.png");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-form {
  position: fixed;
  right: 100px;
  top: 50%;
  transform: translateY(-50%);
  width: 260px;
  padding: 10px;
  border-radius: 10px;
  //background-color: #efefef;
  opacity: 0.8;
  box-shadow: 2px 2px 2px 2px #1e1f1f;

  .login-btn {
    width: 100%;
  }

  .login-title {
    font-size: 19px;
    font-weight: 700;
    text-align: center;
    margin-bottom: 10px;
    color: #efefef;
  }

  .check-code {
    display: flex;

    .input {
      margin-right: 5px;
      width: 55%;
    }

    .pic {
      cursor: pointer;
      height: 30px;
      flex: 1;
    }
  }
}
</style>
